package com.eqdd.yiqidian.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RegistActivityCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.mine.qiye.QiyeRegisterActivity;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.utils.ValidatorUtil;
import com.eqdd.yiqidian.widget.CountDownButtonHelper;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RegistActivity extends BaseActivity {
    private RegistActivityCustom dataBinding;
    private int mode;
    private AlertDialog alertDialog;
    private String msgid;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_regist);
        mode = getIntent().getIntExtra(Config.MODE, -1);
        switch (mode) {
            case 0:
                initTopTitleBar(View.VISIBLE, "个人注册");
                dataBinding.tvInstruction.setVisibility(View.GONE);
                break;
            case 1:
                initTopTitleBar(View.VISIBLE, "公司注册");
                dataBinding.tvInstruction.setVisibility(View.VISIBLE);
                break;
            case 2:
                initTopTitleBar(View.VISIBLE, "忘记密码");
                dataBinding.tvInstruction.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void setView() {
        if (mode == 1) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("企业注册之前，请确认已完成实名认证！");
            builder.setTitle("提示");
            builder.setPositiveButton("确认", (dialog, which) -> {
                dialog.dismiss();
            });
            alertDialog = builder.create();
            alertDialog.show();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_yanzheng:
                String phone = dataBinding.etPhone.getText().toString().trim();
                if (!TextUtils.isEmpty(phone)) {
                    CountDownButtonHelper helper = new CountDownButtonHelper(
                            dataBinding.tvYanzheng, "倒计时", 60, 1);
                    helper.setOnFinishListener(() -> {
                        dataBinding.tvYanzheng.setText("获取验证码");
                        dataBinding.tvYanzheng.setEnabled(true);
                    });
                    helper.start();
                    dataBinding.tvYanzheng.setEnabled(false);
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.SEND_SMS)
                            .params("tel", phone)
                            .execute(new DialogCallBack<HttpResult>(RegistActivity.this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    if (httpResult.getStatus() == 200) {
                                        ToastUtil.showShort("发送验证码成功");

                                        try {
                                            JSONObject jsonObject = new JSONObject(httpResult.getMsg());
                                            msgid = jsonObject.getString("msg_id");
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                        System.out.println(msgid);
                                    }
                                }
                            });
                } else {
                    ToastUtil.showShort("请输入有效的手机号");
                }
                break;
            case R.id.tv_regeist:
                String telphone = dataBinding.etPhone.getText().toString().trim();
                String yzm = dataBinding.etYanzheng.getText().toString().trim();
                if (!TextUtils.isEmpty(telphone) && ValidatorUtil.isMobile(telphone)) {
                    if (!TextUtils.isEmpty(yzm) && yzm.length() == 6) {
                        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CHECK_SMS_CODE)
                                .params("msgid", msgid)
                                .params("code", yzm)
                                .execute(new DialogCallBack<HttpResult>(RegistActivity.this) {
                                    @Override
                                    public void onSuccess(Response<HttpResult> response) {
                                        HttpResult httpResult = response.body();
                                        ToastUtil.showShort(httpResult.getMsg());
                                        if (httpResult.getStatus() == 200) {

                                            enterNextPage();
                                        }
                                    }
                                });
                    } else {
                        ToastUtil.showShort("验证码错误");
                    }
                } else {
                    ToastUtil.showShort("请输入有效的手机号");
                }
        }
    }


    private void enterNextPage() {
        String telphone = dataBinding.etPhone.getText().toString().trim();
        Bundle bundle = new Bundle();
        bundle.putString("phone", telphone);
        switch (mode) {
            case Config.MODE_REGISTER:
                startActivity(RegistTwo.class, bundle);
                finish();
                break;
            case Config.MODE_REGISTER_QIYE:
                OkGo.<HttpResult<User>>post(HttpConfig.BASE_URL + HttpConfig.IS_CAN_REGISTER_COM)
                        .params("username", telphone)
                        .execute(new DialogCallBack<HttpResult<User>>(RegistActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult<User>> response) {
                                HttpResult<User> httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    bundle.putString(Config.GUID,httpResult.getItems().getGuid());
                                    startActivity(QiyeRegisterActivity.class, bundle);
                                    finish();
                                }
                            }
                        });
                break;
            case Config.MODE_PASS_FORGET:
                startActivity(PhonePasswordGetActivity.class, bundle);
                finish();

                break;

        }
    }

    @Override
    protected void onDestroy() {
        // 销毁回调监听接口
        super.onDestroy();
    }
}
